//
//  ChargeViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/15.
//  Copyright © 2016年 apple. All rights reserved.
//
#import "CustomMaskView.h"
#import "ChargeViewController.h"
#import "WebViewController.h"

@interface ChargeViewController ()<UITextFieldDelegate>
{
    UILabel * _phoneLabel;
    UILabel * _leftMoneyLabel;
}
@property (nonatomic,strong) UIView * lineView;
@property (nonatomic,strong) UISegmentedControl * segment;
@property (nonatomic,strong) UIView * personalView;
@property (nonatomic,strong) UILabel * tipLabel;
@property (nonatomic,strong) UITextField * moneyOrPwdtf;
@property (nonatomic,strong) UIView    * chosemoneyView;
@property (nonatomic,strong) UIButton * chargeBtn;
@property (nonatomic,strong) UIButton * detailBtn;
@end

@implementation ChargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = GRAYCOLOR;
    self.title = @"充值中心";
    [self.view addSubview:self.segment];
    [self.view addSubview:self.lineView];
    [self.view addSubview:self.personalView];
    [self.view addSubview:self.tipLabel];
    [self.view addSubview:self.chosemoneyView];
    [self.view addSubview:self.moneyOrPwdtf];
    [self.view addSubview:self.chargeBtn];
    [self.view addSubview:self.detailBtn];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self segmentValueDidChange:_segment];
    _leftMoneyLabel.text = @"余额：0元";
    _phoneLabel.text = @"13837997604";
}
-(void)textFieldDidChangeValued:(UITextField *)textField
{
    //这里进行需要的操作。比如限制输入位数
    if (_segment.selectedSegmentIndex == 0)
    {
        //代表是充值金额
    }else
    {
    //代表是充值卡密码
        
    }
    
}
-(void)chargeMoneyButtonTapped:(UIButton *)btn
{
    if (_segment.selectedSegmentIndex == 0)
    {
        //代表是充值金额，跳转充值界面
        
    }else
    {
        //代表是充值卡密码
        
    }
}
-(void)chargeDetailInstruction
{
    //充值说明
    WebViewController * webVc = [[WebViewController alloc]init];
    webVc.pageUrl = @"http://webapi.msxiyi.com/article/recharge.htm";
    [self.navigationController pushViewController:webVc animated:YES];
}
-(void)segmentValueDidChange:(UISegmentedControl *)seg
{
    _lineView.frame = CGRectMake(_segment.selectedSegmentIndex*kWidth/2.0, CGRectGetMaxY(_segment.frame), kWidth/2.0 , 2);
    
    if (seg.selectedSegmentIndex == 0)
    {
        _tipLabel.text = @"充值金额";
        _chosemoneyView.hidden = NO;
        _moneyOrPwdtf.frame = CGRectMake(0, CGRectGetMaxY(_chosemoneyView.frame)+5, kWidth, 44);
        _moneyOrPwdtf.keyboardType = UIKeyboardTypeDecimalPad;
        _moneyOrPwdtf.placeholder = @"手动输入其它金额";
    }else
    {
        _tipLabel.text = @"充值卡密码";
        _chosemoneyView.hidden = YES;
        _moneyOrPwdtf.frame = CGRectMake(0, CGRectGetMaxY(_tipLabel.frame), kWidth, 44);
        _moneyOrPwdtf.placeholder = @"请输入充值卡密码";
        _moneyOrPwdtf.keyboardType = UIKeyboardTypeDefault;
    }
    _chargeBtn.frame = CGRectMake(10, CGRectGetMaxY(_moneyOrPwdtf.frame)+44, kWidth - 20, 44);
    _detailBtn.frame = CGRectMake(kWidth/2.0 - 100, CGRectGetMaxY(_chargeBtn.frame), 200, 44);
}
-(void)chooseLeftLabel
{
    CustomMaskView * maskView = (CustomMaskView *)[_chosemoneyView viewWithTag:111];
    CGRect rect = maskView.frame;
    rect.origin.x = 0;
    maskView.frame = rect;
}
-(void)chooserightLabel
{
    CustomMaskView * maskView = (CustomMaskView *)[_chosemoneyView viewWithTag:111];
    CGRect rect = maskView.frame;
    rect.origin.x = kWidth/2.0;
    maskView.frame = rect;
}
#pragma mark------------create ui
-(UISegmentedControl *)segment
{
    if (!_segment)
    {
        _segment = [[UISegmentedControl alloc]initWithItems:@[@"在线充值",@"充值卡充值"]];
        _segment.backgroundColor = [UIColor whiteColor];
        _segment.frame = CGRectMake(0, 65, kWidth, 44);
        _segment.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName:BLUECOLOR,NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateSelected];
        [_segment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor],NSFontAttributeName:[UIFont systemFontOfSize:14]} forState:UIControlStateNormal];
        _segment.tintColor = [UIColor whiteColor];
        [_segment addTarget:self action:@selector(segmentValueDidChange:) forControlEvents:UIControlEventValueChanged];
        _segment.selectedSegmentIndex = 0;
    }
    return _segment;
}
-(UIView *)lineView
{
    if (!_lineView)
    {
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = BLUECOLOR;
    }
    return _lineView;
}
-(UIView *)personalView
{
    if (!_personalView)
    {
        _personalView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_segment.frame)+12, kWidth, 44)];
        _personalView.backgroundColor = [UIColor whiteColor];
        _personalView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        
        UIImageView * pepleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 11, 22, 22)];
        pepleImageView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        pepleImageView.image = [UIImage imageNamed:@"peple_number"];
        [_personalView addSubview:pepleImageView];
        
        _phoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(pepleImageView.frame)+5, 0, 110, 44)];
        _phoneLabel.textColor = BLUECOLOR;
        _phoneLabel.font = [UIFont systemFontOfSize:16];
        _phoneLabel.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
        [_personalView addSubview:_phoneLabel];
        
        _leftMoneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(kWidth - 5 -(kWidth - 142 -5), 0, (kWidth - 142 -5), 44)];
        _leftMoneyLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
        _leftMoneyLabel.textColor = [UIColor lightGrayColor];
        _leftMoneyLabel.textAlignment = NSTextAlignmentRight;
        [_personalView addSubview:_leftMoneyLabel];
        
    }
    return _personalView;
}
-(UILabel *)tipLabel
{
    if (!_tipLabel)
    {
        _tipLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(_personalView.frame), kWidth, 44)];
        _tipLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _tipLabel.font = [UIFont systemFontOfSize:16];
    }
    return _tipLabel;
}
-(UITextField *)moneyOrPwdtf
{
    if (!_moneyOrPwdtf)
    {
        _moneyOrPwdtf = [[UITextField alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(_tipLabel.frame), kWidth-10, 44)];
        _moneyOrPwdtf.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _moneyOrPwdtf.backgroundColor = [UIColor whiteColor];
        [_moneyOrPwdtf addTarget:self action:@selector(textFieldDidChangeValued:) forControlEvents:UIControlEventValueChanged];
    }
    return _moneyOrPwdtf;
}
-(UIView *)chosemoneyView
{
    if (!_chosemoneyView)
    {
        _chosemoneyView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_tipLabel.frame), kWidth, 44)];
        _chosemoneyView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        _chosemoneyView.backgroundColor = [UIColor whiteColor];
        
        UILabel * leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, kWidth/2.0-5, 44)];
        leftLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin;
        leftLabel.textColor = BLUECOLOR;
        leftLabel.text = @"充500送120";
        leftLabel.userInteractionEnabled = YES;
        [_chosemoneyView addSubview:leftLabel];
        UITapGestureRecognizer * leftGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseLeftLabel)];
        [leftLabel addGestureRecognizer:leftGesture];
        
        UILabel * rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(5+kWidth/2.0, 0, kWidth/2.0-5, 44)];
        rightLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleRightMargin;
        rightLabel.textColor = BLUECOLOR;
        rightLabel.text = @"充800送260";
        rightLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer * rightGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooserightLabel)];
        [rightLabel addGestureRecognizer:rightGesture];
        
        [_chosemoneyView addSubview:rightLabel];
        
        CustomMaskView * maskView = [[CustomMaskView alloc]initWithFrame:CGRectMake(0, 0, kWidth/2.0, 44)];
        maskView.tag = 111;
        [_chosemoneyView addSubview:maskView];
    }
    return _chosemoneyView;
}
-(UIButton *)chargeBtn
{
    if (!_chargeBtn) {
        _chargeBtn = [[UIButton alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_moneyOrPwdtf.frame)+44, kWidth - 20, 44)];
        _chargeBtn.layer.cornerRadius = 5;
        _chargeBtn.layer.masksToBounds = YES;
        [_chargeBtn setTitle:@"立即充值" forState:UIControlStateNormal];
        [_chargeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _chargeBtn.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        // 89 158 229
        [_chargeBtn addTarget:self action:@selector(chargeMoneyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [_chargeBtn setBackgroundColor:[UIColor colorWithRed:89/255.0 green:158/255.0 blue:229/255.0 alpha:1]];
    }
    return _chargeBtn;
}
-(UIButton *)detailBtn
{
    if (!_detailBtn) {
        _detailBtn = [[UIButton alloc]initWithFrame:CGRectMake(kWidth/2.0 - 100, CGRectGetMaxY(_chargeBtn.frame), 200, 44)];
        [_detailBtn setTitle:@"充值规则说明" forState:UIControlStateNormal];
        [_detailBtn setImage:[UIImage imageNamed:@"wenhao"] forState:UIControlStateNormal];
        [_detailBtn setTitleColor:BLUECOLOR forState:UIControlStateNormal];
        _detailBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_detailBtn addTarget:self action:@selector(chargeDetailInstruction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailBtn;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
